package com.young.manager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.young.common.execption.ServiceException;
import com.young.common.utils.MD5Utils;
import com.young.common.utils.ToolUtils;
import com.young.manager.mapper.CompanyUserMapper;
import com.young.manager.model.CompanyUser;
import com.young.manager.service.CompanyUserService;
import java.util.Date;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@RequiredArgsConstructor
@Service
public class CompanyUserServiceImpl implements CompanyUserService {

  private final CompanyUserMapper companyUserMapper;

  @Override
  public Page<CompanyUser> queryPage(Long companyId, String name, String phone, Integer status, Integer pageNum, Integer pageSize) {
    Page<CompanyUser> page = new Page<>(pageNum, pageSize);
    return companyUserMapper.queryCompanyUserPage(page, companyId, name, phone, status);
  }

  @Override
  public void insertCompanyUser(CompanyUser companyUser) {
    String account = companyUser.getAccount();
    String phone = companyUser.getPhone();
    if (null == account && null == phone) {
      throw new ServiceException("参数缺失");
    }
    if (null == account) {
      companyUser.setAccount(phone);
    }
    QueryWrapper<CompanyUser> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("phone", phone);
    if (companyUserMapper.exists(queryWrapper)) {
      throw new ServiceException("手机号已注册！");
    }
    companyUser.setCreateTime(new Date());
    companyUser.setModifyTime(companyUser.getCreateTime());
    if (null == companyUser.getStatus()) companyUser.setStatus(0);
    if (null == companyUser.getSuperUser()) companyUser.setSuperUser(0);
    if (null == companyUser.getSex()) companyUser.setSex(9);
    String salt = ToolUtils.generateRandomString(4);
    companyUser.setSalt(salt);
    String encPassword = null == companyUser.getPassword() ? MD5Utils.encrypt("123456" + salt) : MD5Utils.encrypt(companyUser.getPassword() + salt);
    companyUser.setPassword(encPassword);
    companyUserMapper.insert(companyUser);
  }

  @Override
  public void updateCompanyUser(CompanyUser companyUser) {
    if (null == companyUser.getId()) {
      throw new ServiceException("参数缺失[id]！");
    }
    QueryWrapper<CompanyUser> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("account", companyUser.getAccount());
    CompanyUser existCompanyUser = companyUserMapper.selectOne(queryWrapper);
    if (null != existCompanyUser && !existCompanyUser.getId().equals(companyUser.getId())) {
      throw new ServiceException("账号已存在！");
    }
    queryWrapper.clear();
    queryWrapper.eq("phone", companyUser.getPhone());
    existCompanyUser = companyUserMapper.selectOne(queryWrapper);
    if (null != existCompanyUser && !existCompanyUser.getId().equals(companyUser.getId())) {
      throw new ServiceException("手机号已被使用！");
    }
    companyUserMapper.updateById(companyUser);
  }

  @Override
  public void resetPassword(Long id, String newPassword) {
    newPassword = null == newPassword ? "123456" : newPassword;
    CompanyUser companyUser = companyUserMapper.selectById(id);
    if (null != companyUser) {
      String salt = ToolUtils.generateRandomString(4);
      companyUser.setSalt(salt);
      companyUser.setPassword(MD5Utils.encrypt(newPassword));
      companyUserMapper.updateById(companyUser);
    }
  }

  @Override
  public void updateCompanyUserStatus(Long id, Integer status) {
    UpdateWrapper<CompanyUser> updateWrapper = new UpdateWrapper<>();
    updateWrapper.eq("id", id);
    updateWrapper.set("status", status);
    companyUserMapper.update(updateWrapper);
  }

  @Override
  public void deleteCompanyUser(Long id) {
    companyUserMapper.deleteById(id);
  }
}
